use super::output::{self, Level};
use std::fmt::{self, Display};

static BUILD_TOOL: &str = "build tool";
static BUILD_CONFIGURATION: &str = "build configuration";
static BUILD_DEPENDENCIES: &str = "build dependencies";
static PACKAGING_PUBLISHING: &str = "packaging and pushlishing";
#[derive(Debug, Default)]
pub struct Detail {
    pub description: String,
    pub level: output::Level,
    pub location: String,
    pub errno: usize,
    pub check_type: String,
    pub line: usize,
}

impl Display for Detail {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{}", self.description)
    }
}

pub enum BuildRule {
    GRS05,
    GRS06,
    GRS08,
    GRS10,
    GRS14,
    GRS17,
    GRS18,
}

impl fmt::Display for BuildRule {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            BuildRule::GRS05 => write!(f, "[G.RS.05]"),
            BuildRule::GRS06 => write!(f, "[G.RS.06]"),
            BuildRule::GRS08 => write!(f, "[G.RS.08]"),
            BuildRule::GRS10 => write!(f, "[G.RS.10]"),
            BuildRule::GRS14 => write!(f, "[G.RS.14]"),
            BuildRule::GRS17 => write!(f, "[G.RS.17]"),
            BuildRule::GRS18 => write!(f, "[G.RS.18]"),
        }
    }
}

impl From<BuildRule> for Detail {
    fn from(value: BuildRule) -> Self {
        match value {
            BuildRule::GRS05 => Detail {
                level: Level::Rule,
                // 用来识别具体规范编码，见[开发预定错误码](https://onebox.huawei.com/v/849036f19356874c5cf463c5c01dfe9d?type=1)
                errno: 31004_usize,
                check_type: String::from(BUILD_TOOL),
                ..Detail::default()
            },
            BuildRule::GRS06 => Detail {
                level: Level::Suggestion,
                errno: 31005_usize,
                check_type: String::from(BUILD_TOOL),
                ..Detail::default()
            },
            BuildRule::GRS08 => Detail {
                level: Level::Rule,
                errno: 31007_usize,
                check_type: String::from(BUILD_CONFIGURATION),
                ..Detail::default()
            },
            BuildRule::GRS10 => Detail {
                level: Level::Rule,
                errno: 31009_usize,
                check_type: String::from(BUILD_CONFIGURATION),
                ..Detail::default()
            },
            BuildRule::GRS14 => Detail {
                level: Level::Rule,
                errno: 31013_usize,
                check_type: String::from(BUILD_DEPENDENCIES),
                ..Detail::default()
            },
            BuildRule::GRS17 => Detail {
                level: Level::Rule,
                errno: 31016_usize,
                check_type: String::from(PACKAGING_PUBLISHING),
                ..Detail::default()
            },
            BuildRule::GRS18 => Detail {
                level: Level::Rule,
                errno: 31017_usize,
                check_type: String::from(PACKAGING_PUBLISHING),
                ..Detail::default()
            },
        }
    }
}

impl Detail {
    pub fn build(
        build_rule: BuildRule,
        location: String,
        description: String,
        line: usize,
    ) -> Self {
        Self {
            location,
            description,
            line,
            ..Detail::from(build_rule)
        }
    }
}
